package caseManagerBusiness;

import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yangxianming on 2018/12/10.
 * 更新用例数据
 */
public class UpdateCaseData implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();

        //获得url参数
        String caseid = request.getParameter("caseid");
        //读取请求body
        String requestInfo = GetRequestInputStream.getString(request);

        //数据库插入 UPDATE testserver.testcase SET `data`='11' WHERE id='6'
        System.out.println(requestInfo);
        //转换单引号 ' -> <yxmsinglequotes>
        requestInfo = requestInfo.replaceAll("'", "<yxmsinglequotes>");
        //转换 \" -> \\" 便于以json方式读取并传到前台
        requestInfo = requestInfo.replaceAll("\\\"", "\\\\\"");

        String set = "`data`='" + requestInfo+ "'";
        String where = "id=\"" + caseid + "\"";
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = sqlDispatch.update("testserver.testcase", set, where);

        returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
        returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        return returnJson.toString();
    }
}
